import java.util.UUID;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class TestJson {

	private String id = null;
	private String name = null;
	private boolean done = false;
	private int intVal;

	public int getIntVal() {
		return intVal;
	}

	public void setIntVal(int intVal) {
		this.intVal = intVal;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public boolean isDone() {
		return done;
	}

	public void setDone(boolean done) {
		this.done = done;
	}

	@Override
	public String toString() {
		return " || " + "[id]: " + id + " || " + "[name]: " + name + " || " + "[done]: "
				+ done + " || " + "[inVal]: " + intVal + " || ";
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		TestJson testJson = new TestJson();
		testJson.setId("New id\n");
		testJson.setName("new name\\/\n");
		testJson.setDone(false);
		testJson.setIntVal(2333);

		// to json string
		String jsonString = JSON.toJSONString(testJson);
		System.out.println(jsonString);
		System.out.println("jsonString size: " + jsonString.length());

		// to json object
		JSONObject jsonObject = JSONObject.parseObject(jsonString);
		System.out.println(jsonObject);
		System.out.println("jsonObject size: " + jsonObject.size());

		// to java object
		TestJson tj = JSON.toJavaObject(jsonObject, TestJson.class);
		System.out.println(tj);

		
		
		 // example #2
		 String string = "Test with reverse solidus." + "\n" + "A new line.";
		 TestJson t2 = new TestJson();
		 t2.setId(UUID.randomUUID().toString());
		 t2.setName(string);
		 System.out.println(JSON.toJSONString(t2));
		
		 //example #3
		 SensorUIOConstant suioConstant = new SensorUIOConstant();
		 String suioJson = JSON.toJSONString(suioConstant);
		 System.out.println(suioJson);
		
		 /* restore a java object from a jsonstring */
		 String jsonString2 = "{\"id\":\"888\", \"name\":\"kjjjj\"}";
		 JSONObject jsonObject2 = JSONObject.parseObject(jsonString2);
		
		 TestJson testJson2 = JSON.toJavaObject(jsonObject2, TestJson.class);
		 System.out.println("JsonString: "+ jsonString2);
		 System.out.println(testJson2);
	}

}
